package wap;

import java.io.File;
import java.util.concurrent.Callable;

import wap.queue.IQueue;

/**
 * @param <E>
 * @描述：生产者线程
 * @作者：hongxingfan
 * @时间：2014年9月19日 上午10:04:16
 */
public class ThreadProduct extends ThreadAbstract implements Callable<String> {

	private String dir;
	private IQueue<String> queue;

	public ThreadProduct(String dir) {
		this.dir = dir;
	}

	public IQueue<String> getQueue() {
		return queue;
	}

	public void setQueue(IQueue<String> queue) {
		this.queue = queue;
	}

	// 没有文件就表示次线程应该结束
	@Override
	public String call() throws Exception {
		int tryCount = 0;
		File file = new File(this.dir);
		while (true) {

			if (tryCount >= 200) {
				System.out.println(Thread.currentThread().getName() + "-->is break!!!");
				break;
			}

			if (file.isDirectory()) {
				for (File filePath : file.listFiles()) {
					try {
						if (this.queue.add(filePath.getAbsolutePath())) {
							tryCount = 0;
							System.out.println("Add-->" + Thread.currentThread().getName() + "-->" + filePath
							        + "-->QueueSize=" + this.queue.queueSize() + "-->HashSize="
							        + this.queue.hashSize());
						} else {
							tryCount++;
							Thread.sleep(1 * 1000);
						}
					} catch (RuntimeException e) {
						e.printStackTrace();
					}
				}
			}
		}
		return null;
	}

	public void recursion(String path) {
		File file = new File(path);
		if (file.isDirectory())
			for (File str : file.listFiles())
				recursion(str.getAbsolutePath());
		else
			System.out.println(file.getAbsolutePath());
	}
}
